<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="分类">
                <a-input allowClear placeholder="请输入分类" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-button type="primary" @click="getList()">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList()">
          <a-radio-button value="-1"> 全部 </a-radio-button>
          <a-radio-button value="1"> 已开启 </a-radio-button>
          <a-radio-button value="0"> 已关闭 </a-radio-button>
        </a-radio-group>

        <div>
          <a-button type="primary" icon="plus" @click="edit()">新建 </a-button>
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="handleAll('change', '1')">批量启用</a-menu-item>
              <a-menu-item @click="handleAll('change', '0')">批量禁用</a-menu-item>
              <a-menu-item @click="handleAll('change', '2')">批量删除</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>
      <a-table
        :rowKey="
          (r, i) => {
            return r.id
          }
        "
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        size="default"
        :columns="columns"
        :data-source="list"
        :customRow="customRow"
        :scroll="{ x: 'max-content' }"
        :pagination="{
          current: pageNo,
          pageSize,
          total,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
      >
        <span slot="name" slot-scope="text">
          <a-tooltip>
            <template slot="title"> {{ text }} </template>
            {{ text }}
          </a-tooltip>
        </span>

        <span slot="is_show" slot-scope="text, record">
          <a-tag v-if="text === '0'" @click="handle('change', record, 1)">已关闭</a-tag>
          <a-tag v-else color="green" @click="handle('change', record, 0)">已开启</a-tag>
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" @click="edit(record)" size="small">编辑</a-button>
          <a-divider type="vertical" />
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;" @click="handle('change', record, 2)">删除</a>
              </a-menu-item>
              <a-menu-item v-if="record.is_show === '0'">
                <a href="javascript:;" @click="handle('change', record, 1)">开启</a>
              </a-menu-item>
              <a-menu-item v-if="record.is_show === '1'">
                <a href="javascript:;" @click="handle('change', record, 0)">关闭</a>
              </a-menu-item>
            </a-menu>
            <a class="ant-dropdown-link"> 更多 <a-icon type="down" /> </a>
          </a-dropdown>
        </span>
      </a-table>

      <a-modal width="700px" v-model="visible" title="新建模版" ok-text="确认" cancel-text="取消" @ok="onSubmit">
        <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-form-model-item label="分类名称" prop="name">
            <a-input v-model="form.name" allowClear placeholder="请输入标题" />
          </a-form-model-item>
          <a-form-model-item label="排序" prop="sort">
            <a-input-number v-model="form.sort" :min="0" :max="100" />
          </a-form-model-item>
          <a-form-model-item label="是否显示" prop="is_show">
            <a-radio-group v-model="form.is_show">
              <a-radio value="1"> 是 </a-radio>
              <a-radio value="0"> 否 </a-radio>
            </a-radio-group>
          </a-form-model-item>
        </a-form-model>
      </a-modal>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { catList, catSave, catStatus, listSelect } from '@/api/marketing/curPrice'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '活动名称',
    dataIndex: 'name',
    scopedSlots: {
      customRender: 'name',
    },
    ellipsis: true,
  },
  {
    title: '排序',
    dataIndex: 'sort',
    sorter: (a, b) => parseFloat(a.sort) - parseFloat(b.sort),
  },
  {
    title: '状态',
    dataIndex: 'is_show',
    scopedSlots: {
      customRender: 'is_show',
    },
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
    width: 200,
  },
]

export default {
  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,
      queryParam: {
        name: '',
        status: '-1',
      },
      list: [],
      selectedRowKeys: [],
      categoryInfo: [],
      visible: false,
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },

      form: {
        name: '',
        sort: 50,
        is_show: '1',
      },

      rules: {
        name: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],
        is_show: [{ required: true, message: '请选择', trigger: 'change' }],
      },
    }
  },

  mounted() {
    this.getList()
  },

  watch: {
    visible(e) {
      if (!e) {
        this.resetForm()
      }
    },
  },

  computed: {
    isShow() {
      return (is_show) => {
        if (String(is_show) === '1') {
          return '已开启'
        } else {
          return '已关闭'
        }
      }
    },
  },

  methods: {
    async edit(item) {
      if (item) {
        let { id, name, sort, is_show } = item

        this.form = {
          id,
          name,
          sort,
          is_show,
        }
      }

      this.visible = true
    },

    onSubmit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          const hide = this.$message.loading('加载中', 0)
          let form = JSON.parse(JSON.stringify(this.form))

          const res = await catSave(form)
          hide()
          if (res.code == 0) {
            this.$message.success('创建成功')
            this.visible = false
            this.getList()

            this.form = {
              name: '',
              sort: 50,
              is_show: '1',
            }
            return
          }
          this.$message.error(res.msg)
        } else {
          return false
        }
      })
    },

    resetForm() {
      this.$refs.ruleForm.resetFields()
    },

    async getList(pageNo = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this
      const res = await catList({
        ...queryParam,
        pageSize,
        pageNo,
      })
      if (res.code == 0) {
        this.pageNo = pageNo
        this.selectedRowKeys = []
        let { totalCount, list: data } = res.data
        this.total = totalCount
        this.list = data
      }
      hide()
    },

    tablePageChange(e) {
      this.getList(e)
    },

    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    handleAll(type, status) {
      let { selectedRowKeys } = this
      if (!selectedRowKeys.length) {
        this.$message.error('请至少选择一条数据~')
        return
      }

      let ids = selectedRowKeys.join(',')

      switch (type) {
        case 'change':
          this.changeStatus(ids, status)
          break
      }
    },

    handle(type, item, status) {
      switch (type) {
        case 'change':
          this.changeStatus(item.id, status)
          break
      }
    },

    changeStatus(ids, status) {
      this.$confirm({
        title: '提示?',
        content: '是否执行此次操作？',
        onOk: () => {
          catStatus({
            ids,
            status,
          }).then((res) => {
            if (res.code) {
              this.$error({
                content: res.msg,
              })
            } else {
              this.$message.success(res.msg)
              this.getList()
              this.selectedRowKeys = []
            }
          })
        },
        onCancel: () => {},
      })
    },
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

::v-deep {
  .ant-tag {
    cursor: pointer;
  }
}
</style>
